package com.yict.csms.system.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.CommonDao;
import com.yict.csms.company.entity.Contract;
import com.yict.csms.system.entity.ExchangeRate;

/**
 * 
 * 
 * @author ryan.wang
 * 
 */
@Repository
public class ExchangeRateDaoImpl extends CommonDao<ExchangeRate, Long> {

	/**
	 * 根据币种查找最大结束日期
	 * @param fromCurrency
	 * @return
	 */
	public Date findByCurrency(String fromCurrency) {
		Date result = null;
		StringBuilder queryString = new StringBuilder();
		String field = " order by c.endDate desc";
		queryString.append("select max(c.endDate) from ExchangeRate as c where 1=1 and c.fromCurrency.dictid = :fromCurrency");
		queryString.append(field);
		
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("fromCurrency", fromCurrency);
		
		try {
			Query query = this.getSession().createQuery(queryString.toString());
			query.setParameter("fromCurrency", fromCurrency);
			List<Date> list = query.list();
			if (list != null && list.size() > 0) {
				result = list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public ExchangeRate findById(Long id){
		return (ExchangeRate)this.getTemplate().get(ExchangeRate.class, id);
	}

}
